package br.usp.ime.ingpos.services;

import java.io.IOException;

import org.apache.commons.io.IOUtils;

import br.com.caelum.vraptor.interceptor.multipart.DefaultMultipartConfig;
import br.com.caelum.vraptor.interceptor.multipart.UploadedFile;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.RequestScoped;

@Component
@RequestScoped
public class UploadService
    extends
        DefaultMultipartConfig
{
    private final int MAX_FILE_SIZE = 15 * 1024 * 1024; // 10MB;

    public UploadService()
    {
    }

    public byte[] convertePDFParaByteArray(
        UploadedFile file )
        throws IOException
    {
        return IOUtils.toByteArray( file.getFile() );
    }

    public long getSizeLimit()
    {
        return MAX_FILE_SIZE;
    }

    public String getNomeDoArquivoSemExtensao(
        UploadedFile file )
    {
        return file.getFileName().substring( 0, file.getFileName().lastIndexOf( "." ) );
    }
}
